

$(function(){

	function changeMode(mode){
		var modeType = window.__PLAYER__.PlayerData.changeMode();
		for (; modeType != mode; ) {
			modeType = window.__PLAYER__.PlayerData.changeMode();
		}
		window.__PLAYER__.setModeView(mode), setTimeout(function() {
				window.__PLAYER__.Audio.mode(mode);
		}, 0);
	}

	function processMsg(msg) {
		var data = JSON.parse(msg);
		if (data.type == "play_control") {
			if (data.msg == "play") {
				window.__PLAYER__.playOrPause();
			} else if (data.msg == "next") {
				window.__PLAYER__.next();
			} else if (data.msg == "prev") {
				window.__PLAYER__.prev();
			} else if (data.msg == "up") {
				window.__PLAYER__.PlayerVolume.volumeUP()
			} else if (data.msg == "down") {
				window.__PLAYER__.PlayerVolume.volumeDOWN()
			}
		} else if (data.type == "mode_control") {
			if (data.msg == "order") {
				changeMode(1);
			} else if (data.msg == "random") {
				changeMode(2);
			} else if (data.msg == "only") {
				changeMode(0);
			} else {
				changeMode(1);
			}
		}
	}

	var oldstatus = "";
	var oldmode = "";
	function PlayStatus() {
		var status = $('#J_playBtn').attr('class');
		if (status != undefined && oldstatus != status) {
			oldstatus = status;
			var data = {
				type: "play_status",
				status: status
			};
			sendMessage(JSON.stringify(data));
		}
		var mode = $('#J_playerMode').attr('class');
		if (mode != undefined && oldmode != mode){
			oldmode = mode;
			var data = {
				type: "play_mode",
				mode: mode
			};
			sendMessage(JSON.stringify(data));
		}
		setTimeout(PlayStatus, 1000);
	}


	/*跨域通讯*/
	window.Messenger=function(){var prefix="[PROJECT_NAME]",supportPostMessage="postMessage"in window;function Target(target,name){var errMsg="";if(arguments.length<2){errMsg="target error - target and name are both requied"}else if(typeof target!="object"){errMsg="target error - target itself must be window object"}else if(typeof name!="string"){errMsg="target error - target name must be string type"}if(errMsg){throw new Error(errMsg)}this.target=target;this.name=name}if(supportPostMessage){Target.prototype.send=function(msg){this.target.postMessage(prefix+msg,"*")}}else{Target.prototype.send=function(msg){var targetFunc=window.navigator[prefix+this.name];if(typeof targetFunc=="function"){targetFunc(prefix+msg,window)}else{throw new Error("target callback function is not defined")}}}function Messenger(messengerName,projectName){this.targets={};this.name=messengerName;this.listenFunc=[];prefix=projectName||prefix;this.initListen()}Messenger.prototype.addTarget=function(target,name){var targetObj=new Target(target,name);this.targets[name]=targetObj};Messenger.prototype.initListen=function(){var self=this;var generalCallback=function(msg){if(typeof msg=="object"&&msg.data){msg=msg.data}msg=msg.slice(prefix.length);for(var i=0;i<self.listenFunc.length;i++){self.listenFunc[i](msg)}};if(supportPostMessage){if("addEventListener"in document){window.addEventListener("message",generalCallback,false)}else if("attachEvent"in document){window.attachEvent("onmessage",generalCallback)}}else{window.navigator[prefix+this.name]=generalCallback}};Messenger.prototype.listen=function(callback){this.listenFunc.push(callback)};Messenger.prototype.clear=function(){this.listenFunc=[]};Messenger.prototype.send=function(msg){var targets=this.targets,target;for(target in targets){if(targets.hasOwnProperty(target)){targets[target].send(msg)}}};return Messenger}();
	var messenger = new Messenger('iframe_xiami');
	messenger.listen(function (msg) {
		processMsg(msg);
	});
	messenger.addTarget(window.parent, 'parent');

	function sendMessage(data) {
		messenger.targets['parent'].send(data);
	}

	PlayStatus();
});